<template>
    <el-container class="home-container">
<!--      头部-->
      <el-header>
        <div>
          <img class="logo" src="../assets/img/logo.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
<!--      主题-->
      <el-container>
<!--        侧边栏-->
        <el-aside :width="isCollapse ? '75px' : '200px'">
          <div class="toggle-button" @click="togglecoll">|||</div>
          <el-menu router :collapse-transition="false" :collapse="isCollapse"
                   unique-opened background-color="#313743" text-color="#fff" active-text-color="#359bff"
          :default-active="activePath">
<!--            一级菜单-->
            <el-submenu v-for="item in menulist" :key="item.id" :index="item.id + ''">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
<!--              二级菜单-->
              <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/' + subItem.path"
              @click="saveNavstate('/' + subItem.path)">
                <template slot="title">
                  <i class="el-icon-s-grid"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
          <div class="bottom_font bottom_text1">Design    By</div>
          <div class="bottom_font bottom_text2">CodeLing</div>
        </el-aside>
<!--        主体-->
        <el-main>
<!--          路由占位符-->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单
      menulist: [],
      iconObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-platform',
        101: 'el-icon-s-cooperation',
        102: 'el-icon-s-order',
        145: 'el-icon-s-data'
      },
      // 是否则爹
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出成功')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 点击折叠菜单
    togglecoll () {
      this.isCollapse = !this.isCollapse
    },
    // 连接激活状态
    saveNavstate (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style scoped>
.el-header{
  background-color: #313743;
  box-shadow: 10px 10px 10px #b2b2b2;
  z-index: 99;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #F2F2F2;
  font-size: 20px;
}
.el-header div{
  display: flex;
  align-items: center;
}
.el-header button:hover{
  background-color: #b9def0;
  color: lightpink;
}
.el-aside{
  background-color: #313743;
  box-shadow: 10px 10px 10px #b2b2b2;
  z-index: 100;
}
.el-main{
  background-color: #e9edf0;
}
.home-container{
  height: 100%;
}
.logo{
  width: 70px;
  height: 70px;
  cursor: pointer;
}
i{
  margin-right: 10px;
}
.el-menu{
  border-right: none;
}
.bottom_font{
  margin-top: 10px;
  color: lightgray;
  text-align: center;
  cursor: pointer;
}
.bottom_text1{
  margin-top: 200px;
}
.toggle-button{
  background-color: #475163;
  color: white;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
